from django.db import models

from browsertests.tests.models import Test
from browsertests.useragents.models import Useragent

class Result(models.Model):
    STATUS_CHOICE = (
        ('PASS', 'PASS'),
        ('FAIL', 'FAIL'),
        ('FAIL-SINGLE-WINDOW', 'FAIL-SINGLE-WINDOW'),
        ('FAIL-SCREENSHOT', 'FAIL-SCREENSHOT'),
        ('HANG', 'HANG'),
        ('CRASH', 'CRASH'),
    )
    # XXX redundancy, type = test.type + "result"
    type = models.CharField(max_length=60)
    test = models.ForeignKey(Test)
    test_version = models.IntegerField(default=0)
    ua = models.ForeignKey(Useragent)
    date = models.DateTimeField(auto_now_add=True)
    duration = models.FloatField(null=True, blank=True)
    session = models.CharField(max_length=60, blank=True)
    # XXX redundancy, succeeded = (status == PASS)
    succeeded = models.BooleanField(blank=True)
    status = models.CharField(max_length=60, choices=STATUS_CHOICE,
                              default="FAIL")
    # mochitests
    log = models.TextField(blank=True)
    pass_count = models.IntegerField(default=0)
    fail_count = models.IntegerField(default=0)
    todo_count = models.IntegerField(default=0)
    # layouttests
    diff = models.TextField(blank=True)
    # reftests
    pixel_diff = models.IntegerField(default=0)
    saved_path = models.CharField(max_length=300, blank=True)

    class Meta:
        unique_together = (("test", "test_version", "ua"),)

    def __unicode__(self):
        return "Test %s with ua %s status %s" % (self.test, self.ua,
                                                 "PASS" if self.succeeded else "FAIL")
    @models.permalink
    def get_absolute_url(self):
        return ('result-detail', [str(self.id)])

try:
    import browsertests.main.utils
    browsertests.main.utils.redirect___file__(globals())
except:
    pass
